/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import ejb.Company;
import ejb.CompanyFacadeLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author longdm0708l
 */
public class CompanyBean {
    public void createCompany(Company company)
    {
        lookupCompanyFacade().create(company);
    }

    public void editCompany(Company company)
    {
        lookupCompanyFacade().edit(company);
    }

    public void removeCompany(Company company)
    {
        lookupCompanyFacade().remove(company);
    }

    private int companyId;

    public int getCompanyId() {
        return companyId;
    }

    public void setCompanyId(int companyId) {
        this.companyId = companyId;
    }

    public List<Company> getAllCompany()
    {
        return lookupCompanyFacade().findAll();
    }

    public Company getCompanyById()
    {
        return lookupCompanyFacade().find(Integer.valueOf(getCompanyId()));
    }

    private CompanyFacadeLocal lookupCompanyFacade() {
        try {
            Context c = new InitialContext();
            return (CompanyFacadeLocal) c.lookup("java:comp/env/CompanyFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
